package com.koron.order.LineHomepage.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.koron.common.core.business.configsettings.utils.ConfigInfoTool;
import com.koron.common.core.util.CodeTools;
import com.koron.order.LineHomepage.bean.VO.AnalysisReportVO;
import com.koron.order.LineHomepage.bean.VO.ReportGenerationVO;
import com.koron.order.LineHomepage.bean.query.LineOperationAnalysisQuery;
import com.koron.order.LineHomepage.mapper.LineOperationAnalysisMapper;
import com.koron.order.LineHomepage.service.LineOperationAnalysisService;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: LineOperationAnalysisServiceImpl
 * @Description:
 * @Date: 2023/2/21
 * @Author: 李一辰
 * @Version: 1.0
 **/
@Service
public class LineOperationAnalysisServiceImpl implements LineOperationAnalysisService {


    @Override
    @TaskAnnotation("reportGeneration")
    public List<ReportGenerationVO> reportGeneration(SessionFactory sessionFactory, LineOperationAnalysisQuery analysisQuery) {
        if(analysisQuery.getDate() == null ){
            analysisQuery.setDate(new Date());
        }
        LineOperationAnalysisMapper mapper = sessionFactory.getMapper(LineOperationAnalysisMapper.class);
        //缺陷新发现量(不停电处理)
        List<AnalysisReportVO> defectNonCut = mapper.getDefect00And10(analysisQuery);
        //缺陷新发现量(停电处理)
        List<AnalysisReportVO> defectCut = mapper.getDefect01And11(analysisQuery);
        //缺陷新发现量(总缺陷数)
        List<AnalysisReportVO> defectCount = mapper.getDefectCount(analysisQuery);
        //上月缺陷发现量(不停电处理)
        List<AnalysisReportVO> defectNonCutLast = mapper.getDefectTotalLastMonth(analysisQuery);
        //上月缺陷发现量(停电处理)
        List<AnalysisReportVO> defectCutLast = mapper.getDefectTotal00And10LastMonth(analysisQuery);
        //上月缺陷发现量(总缺陷数)
        List<AnalysisReportVO> defectCountLast= mapper.getDefectTotal01And11LastMonth(analysisQuery);
        //本月缺陷总数
        List<AnalysisReportVO> defectTotalThis = mapper.getDefectTotalThisMonth(analysisQuery);

        List<AnalysisReportVO> defectTotal00And10ThisMonth = mapper.getDefectTotal00And10ThisMonth(analysisQuery);

        List<AnalysisReportVO> defectTotal01And11ThisMonth = mapper.getDefectTotal01And11ThisMonth(analysisQuery);

        List<AnalysisReportVO> finish00And10 = mapper.getFinish00And10(analysisQuery);

        List<AnalysisReportVO> finish01And11 = mapper.getFinish01And11(analysisQuery);
        //    消缺情况：统计已归档的缺陷工单归档时间为筛选日期范围的总数
        List<AnalysisReportVO> finishOnTime = mapper.getFinishOnTime(analysisQuery);
        //统计线路跳闸报告
        List<AnalysisReportVO> lineTripCount = mapper.getLineTripCount(analysisQuery);

        Map<String, String> config = ConfigInfoTool.getConfigByCode("equipment", "line_root_device");
        String id = config.get("id");
        List<String> lineEquipment = mapper.getLineEquipment(id);
        ArrayList<ReportGenerationVO> list = new ArrayList<>();
        for (String equipmentName : lineEquipment) {
            ReportGenerationVO reportGenerationVO = new ReportGenerationVO();
//
            AnalysisReportVO defect0 =   defectNonCut.stream().filter(i -> equipmentName.equals(i.getEquipmentName())).findFirst().orElse(new AnalysisReportVO());
            AnalysisReportVO defect1 = defectCut.stream().filter(i -> equipmentName.equals(i.getEquipmentName())).findFirst().orElse(new AnalysisReportVO());
            AnalysisReportVO defectThis = defectCount.stream().filter(i -> equipmentName.equals(i.getEquipmentName())).findFirst().orElse(new AnalysisReportVO());
            AnalysisReportVO defectNonLast = defectNonCutLast.stream().filter(i -> equipmentName.equals(i.getEquipmentName())).findFirst().orElse(new AnalysisReportVO());
            AnalysisReportVO defectCLast = defectCutLast.stream().filter(i -> equipmentName.equals(i.getEquipmentName())).findFirst().orElse(new AnalysisReportVO());
            AnalysisReportVO defectLast = defectCountLast.stream().filter(i -> equipmentName.equals(i.getEquipmentName())).findFirst().orElse(new AnalysisReportVO());
            AnalysisReportVO ThisTotal = defectTotalThis.stream().filter(i -> equipmentName.equals(i.getEquipmentName())).findFirst().orElse(new AnalysisReportVO());
            AnalysisReportVO This00 = defectTotal00And10ThisMonth.stream().filter(i -> equipmentName.equals(i.getEquipmentName())).findFirst().orElse(new AnalysisReportVO());
            AnalysisReportVO This01 = defectTotal01And11ThisMonth.stream().filter(i -> equipmentName.equals(i.getEquipmentName())).findFirst().orElse(new AnalysisReportVO());
            AnalysisReportVO finish00 = finish00And10.stream().filter(i -> equipmentName.equals(i.getEquipmentName())).findFirst().orElse(new AnalysisReportVO());
            AnalysisReportVO finish01 = finish01And11.stream().filter(i -> equipmentName.equals(i.getEquipmentName())).findFirst().orElse(new AnalysisReportVO());
            AnalysisReportVO finish = finishOnTime.stream().filter(i -> equipmentName.equals(i.getEquipmentName())).findFirst().orElse(new AnalysisReportVO());
            AnalysisReportVO lineTrip = lineTripCount.stream().filter(i -> equipmentName.equals(i.getEquipmentName())).findFirst().orElse(new AnalysisReportVO());
            reportGenerationVO.setId(CodeTools.getCode32());
            reportGenerationVO.setEquipmentName(equipmentName);
            reportGenerationVO.setEquipmentId(defect0.getEquipmentId());
            reportGenerationVO.setDefect00New(defect1.getCount());
            reportGenerationVO.setDefect01New(defect0.getCount());
            reportGenerationVO.setDefectNew(defectThis.getCount());
            reportGenerationVO.setDefectLast01(defectNonLast.getCount());
            reportGenerationVO.setDefectLast00(defectCLast.getCount());
            reportGenerationVO.setDefectTotalLast(defectLast.getCount());
            reportGenerationVO.setDefectTotalThis(ThisTotal.getCount());
            reportGenerationVO.setDefectThis00(This00.getCount());
            reportGenerationVO.setDefectThis01(This01.getCount());
            reportGenerationVO.setDefectTotal(finish.getCount());
            reportGenerationVO.setDefect00(finish00.getCount());
            reportGenerationVO.setDefect01(finish01.getCount());
            reportGenerationVO.setTripTimes(lineTrip.getCount());
            reportGenerationVO.setRecordingTime(new Date());
            list.add(reportGenerationVO);
        }

        return list;
    }

    @Override
    @TaskAnnotation("insertBatch")
    public List<ReportGenerationVO> insertBatch(SessionFactory sessionFactory, List<ReportGenerationVO> list) {
        LineOperationAnalysisMapper mapper = sessionFactory.getMapper(LineOperationAnalysisMapper.class);
        if(CollectionUtil.isEmpty(list)){
            return  new ArrayList<>();
        }
        list.stream().forEach(i ->i.setRecordFlag());
        mapper.insertBatch(list);
        return list;
    }

    @Override
    @TaskAnnotation("selectList")
    public List<ReportGenerationVO> selectList(SessionFactory sessionFactory, Date date ,List<String> equipmentNames) {
        LineOperationAnalysisMapper mapper = sessionFactory.getMapper(LineOperationAnalysisMapper.class);
        return mapper.selectList(date,equipmentNames);
    }
}
